<?php
class ControllerShippingMultiFlat extends Controller {
	private $error = array(); 
	
	public function index() {   
		$this->language->load('shipping/multiflat');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('setting/setting');
				
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$arrayOld = $this->request->post;
			$arrayNew['multiflat_cost'] = '';
			$arrayNew['multiflat_tax_class_id'] = '';
			$arrayNew['multiflat_geo_zone_id'] = '';
			$arrayNew['multiflat_status'] = '';
			$arrayNew['multiflat_sort_order'] = '';
			
			$this->load->model('setting/store');
			$stores = $this->model_setting_store->getStores();
			foreach($stores as $store)
			{
				if(isset($arrayOld['multiflat_'.$store['store_id']])) {
					for($i = 0; $i < $arrayOld['multiflat_'.$store['store_id']]; $i++) {
						if(isset($arrayOld['multiflat_cost_'.$store['store_id'].'_'.$i]) && $arrayOld['multiflat_cost_'.$store['store_id'].'_'.$i] !== '') {
							$arrayNew['multiflat_cost'] .= $arrayOld['multiflat_cost_'.$store['store_id'].'_'.$i] . ':';
							$arrayNew['multiflat_tax_class_id'] .= $arrayOld['multiflat_tax_class_id_'.$store['store_id'].'_'.$i] . ':';
							$arrayNew['multiflat_geo_zone_id'] .= $arrayOld['multiflat_geo_zone_id_'.$store['store_id'].'_'.$i] . ':';
							$arrayNew['multiflat_status'] .= $arrayOld['multiflat_status_'.$store['store_id'].'_'.$i] . ':';
							$arrayNew['multiflat_sort_order'] .= (!empty($arrayOld['multiflat_sort_order_'.$store['store_id'].'_'.$i]) ? $arrayOld['multiflat_sort_order_'.$store['store_id'].'_'.$i] : '0') . ':';
						}
					}
				}
				$arrayNew['multiflat_cost'] = trim($arrayNew['multiflat_cost'], ":");
				$arrayNew['multiflat_tax_class_id'] = trim($arrayNew['multiflat_tax_class_id'], ":");
				$arrayNew['multiflat_geo_zone_id'] = trim($arrayNew['multiflat_geo_zone_id'], ":");
				$arrayNew['multiflat_status'] = trim($arrayNew['multiflat_status'], ":");
				$arrayNew['multiflat_sort_order'] = trim($arrayNew['multiflat_sort_order'], ":");
				$this->model_setting_setting->editSetting('multiflat', $arrayNew, $store['store_id']);
				
				unset($this->request->post['multiflat_cost_'.$store['store_id']]);
				unset($this->request->post['multiflat_tax_class_id_'.$store['store_id']]);
				unset($this->request->post['multiflat_geo_zone_id_'.$store['store_id']]);
				unset($this->request->post['multiflat_status_'.$store['store_id']]);
				unset($this->request->post['multiflat_sort_order_'.$store['store_id']]);
			}
			
			$arrayNew['multiflat_cost'] = '';
			$arrayNew['multiflat_tax_class_id'] = '';
			$arrayNew['multiflat_geo_zone_id'] = '';
			$arrayNew['multiflat_status'] = '';
			$arrayNew['multiflat_sort_order'] = '';
			if(isset($arrayOld['multiflat_0'])) {
				for($i = 0; $i < $arrayOld['multiflat_0']; $i++) {
						if(isset($arrayOld['multiflat_cost_0_'.$i]) && $arrayOld['multiflat_cost_0_'.$i] !== '') {
						@$arrayNew['multiflat_cost'] .= $arrayOld['multiflat_cost_0_'.$i] . ':';
						@$arrayNew['multiflat_tax_class_id'] .= $arrayOld['multiflat_tax_class_id_0_'.$i] . ':';
						@$arrayNew['multiflat_geo_zone_id'] .= $arrayOld['multiflat_geo_zone_id_0_'.$i] . ':';
						@$arrayNew['multiflat_status'] .= $arrayOld['multiflat_status_0_'.$i] . ':';
						@$arrayNew['multiflat_sort_order'] .= (!empty($arrayOld['multiflat_sort_order_0_'.$i]) ? $arrayOld['multiflat_sort_order_0_'.$i] : '0') . ':';
					}
				}
			}
			$arrayNew['multiflat_cost'] = trim($arrayNew['multiflat_cost'], ":");
			$arrayNew['multiflat_tax_class_id'] = trim($arrayNew['multiflat_tax_class_id'], ":");
			$arrayNew['multiflat_geo_zone_id'] = trim($arrayNew['multiflat_geo_zone_id'], ":");
			$arrayNew['multiflat_status'] = trim($arrayNew['multiflat_status'], ":");
			$arrayNew['multiflat_sort_order'] = trim($arrayNew['multiflat_sort_order'], ":");
			$this->model_setting_setting->editSetting('multiflat', $arrayNew);
					
			$this->session->data['success'] = $this->language->get('text_success');
						
			$this->redirect($this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL'));
		}
				
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_all_zones'] = $this->language->get('text_all_zones');
		$this->data['text_none'] = $this->language->get('text_none');
		
		$this->data['entry_store'] = $this->language->get('entry_store');
		$this->data['entry_cost'] = $this->language->get('entry_cost');
		$this->data['entry_tax_class'] = $this->language->get('entry_tax_class');
		$this->data['entry_geo_zone'] = $this->language->get('entry_geo_zone');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_module'] = $this->language->get('button_add_module');
		$this->data['button_remove'] = $this->language->get('button_remove');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_shipping'),
			'href'      => $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('shipping/multiflat', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('shipping/multiflat', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL');
		
		// get store items
		$this->load->model('setting/store');
		
		$this->data['stores'] = $this->model_setting_store->getStores();
		$array = array();
		foreach($this->data['stores'] as $store)
		{
			if (isset($this->request->post['multiflat_cost_'.$store['store_id']])) {
				$this->data['multiflat_cost_'.$store['store_id']] = $this->request->post['multiflat_cost_'.$store['store_id']];
			} else {
				$setting = $this->model_setting_setting->getSetting('multiflat', $store['store_id']);
				$array[$store['store_id']]['multiflat_cost'] = $setting['multiflat_cost'];
			}
			
			if (isset($this->request->post['multiflat_tax_class_id_'.$store['store_id']])) {
				$this->data['multiflat_tax_class_id'] = $this->request->post['multiflat_tax_class_id_'.$store['store_id']];
			} else {
				$setting = $this->model_setting_setting->getSetting('multiflat', $store['store_id']);
				$array[$store['store_id']]['multiflat_tax_class_id'] = $setting['multiflat_tax_class_id'];
			}
					
			if (isset($this->request->post['multiflat_geo_zone_id_'.$store['store_id']])) {
				$this->data['multiflat_geo_zone_id'] = $this->request->post['multiflat_geo_zone_id_'.$store['store_id']];
			} else {
				$setting = $this->model_setting_setting->getSetting('multiflat', $store['store_id']);
				$array[$store['store_id']]['multiflat_geo_zone_id'] = $setting['multiflat_geo_zone_id'];
			}
			
			if (isset($this->request->post['multiflat_status_'.$store['store_id']])) {
				$this->data['multiflat_status'] = $this->request->post['multiflat_status_'.$store['store_id']];
			} else {
				$setting = $this->model_setting_setting->getSetting('multiflat', $store['store_id']);
				$array[$store['store_id']]['multiflat_status'] = $setting['multiflat_status'];
			}

			if (isset($this->request->post['multiflat_sort_order_'.$store['store_id']])) {
				$this->data['multiflat_sort_order'] = $this->request->post['multiflat_sort_order_'.$store['store_id']];
			} else {
				$setting = $this->model_setting_setting->getSetting('multiflat', $store['store_id']);
				$array[$store['store_id']]['multiflat_sort_order'] = $setting['multiflat_sort_order'];
			}	
			$this->data['store_array'] = $array;
		}
		
		if (isset($this->request->post['multiflat_cost'])) {
			$this->data['multiflat_cost'] = $this->request->post['multiflat_cost'];
		} else {
			$this->data['multiflat_cost'] = $this->config->get('multiflat_cost');
		}

		if (isset($this->request->post['multiflat_tax_class_id'])) {
			$this->data['multiflat_tax_class_id'] = $this->request->post['multiflat_tax_class_id'];
		} else {
			$this->data['multiflat_tax_class_id'] = $this->config->get('multiflat_tax_class_id');
		}
		
		$this->load->model('localisation/tax_class');
		
		$this->data['tax_classes'] = $this->model_localisation_tax_class->getTaxClasses();

		if (isset($this->request->post['multiflat_geo_zone_id'])) {
			$this->data['multiflat_geo_zone_id'] = $this->request->post['multiflat_geo_zone_id'];
		} else {
			$this->data['multiflat_geo_zone_id'] = $this->config->get('multiflat_geo_zone_id');
		}
		
		$this->load->model('localisation/geo_zone');
		
		$this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
		
		if (isset($this->request->post['multiflat_status'])) {
			$this->data['multiflat_status'] = $this->request->post['multiflat_status'];
		} else {
			$this->data['multiflat_status'] = $this->config->get('multiflat_status');
		}
		
		if (isset($this->request->post['multiflat_sort_order'])) {
			$this->data['multiflat_sort_order'] = $this->request->post['multiflat_sort_order'];
		} else {
			$this->data['multiflat_sort_order'] = $this->config->get('multiflat_sort_order');
		}				

		$this->template = 'shipping/multiflat.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
	
	protected function validate() {
		if (!$this->user->hasPermission('modify', 'shipping/multiflat')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->error) {
			return true;
		} else {
			return false;
		}	
	}
}
?>